package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.custom.Vo.CommentVo;
import com.future.hair.server.custom.Vo.WorksVo;
import com.future.hair.server.entity.User;
import com.future.hair.server.entity.Works;
import com.future.hair.server.entity.WorksComment;
import com.future.hair.server.mapper.UserMapper;
import com.future.hair.server.mapper.WorksCommentMapper;
import com.future.hair.server.mapper.WorksMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("//hair/commentVo")
@Api(tags = "commentVo管理接口")
public class CommentVoController {

    @Autowired
    private WorksCommentMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "查询 CommentVo 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<CommentVo>> worksVoList(@RequestBody PageModel data) {
        if (null==data.getId()){
            return CommonResult.failed("作品id不能为空");
        }
        List<CommentVo> commentVoList = new ArrayList<CommentVo>();
        List<WorksComment> worksCommentList = worksCommentList(data);
        for (WorksComment info:
                worksCommentList) {
            commentVoList.add(getCommentVo(info));
        }
        return CommonResult.data(commentVoList);
    }


    public List<WorksComment> worksCommentList(PageModel data) {
        QueryWrapper<WorksComment> commentWrapper = new QueryWrapper<WorksComment>();
        commentWrapper.eq("works_id",data.getId());
        return mapper.selectPage(new Page<>(data.getSize(), data.getCurrent()), commentWrapper).getRecords();
    }


    public CommentVo getCommentVo(WorksComment data){
        if (null==data){
            return null;
        }

        CommentVo commentVo = new CommentVo();

        commentVo.setId(data.getId());
        commentVo.setUserId(data.getUserId());
        commentVo.setWorksId(data.getWorksId());
        commentVo.setComment(data.getComment());
        commentVo.setCreateTime(data.getCreateTime());

        User user = userMapper.selectById(data.getUserId());
        commentVo.setAvatar(user.getAvatar());
        commentVo.setUserName(user.getUsername());

        return commentVo;
    }


}
